import urllib2
import urlparse
import sys
from httplib import HTTP
from xml.dom import minidom

def checkURL(url):
	try:
		p = urlparse(url)
		h = HTTP(p[1])
		h.putrequest('HEAD', p[2])
		h.endheaders()	
		if h.getreply()[0] == 200:
			return 1
		else:
			return 0
		
	except:
		return 1

def download(URL, name):
	print "[*] Fetching malicious URLs from " + name + " . . ."
	
	# Grab the malicious list
	url 	 	   = urlparse.urlparse(URL)
	response 	   = urllib2.urlopen(URL)
	
	return response
	
def blade_defender_org():
	domain   = "http://www.blade-defender.org/eval-lab/blade.csv"
	response = download(domain, "Blade Defender")
	
	domains = []
	
	for line in response:
		domain_data = line.split()
		domain      = domain_data[len(domain_data) - 1]
		domain		= domain.split('"')
		domain		= domain[1]
		domain		= domain.replace("hxxp", "http")
		if checkURL(domain):
			domains.append(domain)
		
	domains = list(set(domains))
	
	print "[*] Found " + str(len(domains)) + " domains"
	
	return domains

def malware_domains_com():
	domain   = "http://www.malwaredomains.com/files/domains.txt"
	response = download(domain, "Malware Domains")

	domains = []

	for line in response.readlines():
		if line[0] != "#":
			domain_data = line.split()
			domain		= domain_data[0]
			if checkURL(domain):
				domains.append(domain)
		
	domains = list(set(domains))
	
	print "[*] Found " + str(len(domains)) + " domains"
	
	return domains
	
def malware_domains_list_com():
	xmldoc_remote = download("http://www.malwaredomainlist.com/hostslist/mdl.xml", "Malware Domain List")
	xmldoc  = minidom.parse(xmldoc_remote)
	nameEls = xmldoc.getElementsByTagName('description')
	domains   = []
	for el in nameEls:
		el = el.childNodes[0].nodeValue
		el = el.split(",")
		el = el[0]
		el = el.replace("Host: ", "")
		el = "http://" + el
		domains.append(el)
	
	print "[*] Found " + str(len(domains)) + " domains"
	
	return domains
	
def scrape_malware_domains():
	md = malware_domains_com()
	bd = blade_defender_org()
	dl = malware_domains_list_com()
	
	return dl + md + bd
